#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/timer.h>

/**
 *
 * dts :
 my_platform_device003 {
 status = "okay";
 compatible ="my_platform_device_003";
 };

 how  to test:
 tree  /sys/input/
 sudo  hexdumop  /sys/input/eventX


 */

static struct input_dev *button_dev;

bool a = true;

static struct timer_list my_timer;

static void my_timer_func(unsigned long unused) {
	pr_info("timer expires , now  do sth 23333\n");
	a = !a;
	pr_info("input_report_key %d  \n", a);
	input_report_key(button_dev, BTN_0, a);
	input_report_rel(button_dev, REL_X, 0x12);
	input_sync(button_dev);
	pr_info("button_interrupt start \n");

	if (!timer_pending(&my_timer)) {
		mod_timer(&my_timer, jiffies + msecs_to_jiffies(500));
	}
}

int myprobe(struct platform_device *pdev) {
	int ret;
	pr_info("myplatformdriver myprobe \n");

	button_dev = input_allocate_device();
	if (!button_dev) {
		printk(KERN_ERR "button.c: Not enough memory\n");
		return -ENOMEM;
	}
	button_dev->name = "my simple key2";

	set_bit(EV_KEY, button_dev->evbit);
	set_bit(EV_REL, button_dev->evbit);

	set_bit(REL_X, button_dev->relbit);
	set_bit(BTN_0, button_dev->keybit);

	button_dev->dev.parent = &pdev->dev;
	ret = input_register_device(button_dev);
	if (ret) {
		printk(KERN_ERR "button.c: Failed to register device\n");
		goto err_free_dev;
	}

	my_timer.function = my_timer_func;
	my_timer.expires = jiffies + msecs_to_jiffies(500);
	my_timer.data = 0;
	init_timer(&my_timer);
	add_timer(&my_timer);

	return 0;
	err_free_dev: input_free_device(button_dev);
	return ret;
}

int myremove(struct platform_device *pdev) {
	del_timer(&my_timer);
	input_unregister_device(button_dev);
	input_free_device(button_dev);
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver =
		{ .of_match_table = &my_of_match_table, .name = "my-platform-driver",
				.owner = THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
